﻿//using System;
//using System.Collections;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Drawing;

//using LowLevelGraphics;

//namespace LowLevelGraphics.Extractors
//{
//    /// <summary>
//    /// Extract blobs
//    /// </summary>
//    public class BlobExtractor : IEnumerable 
//    {
//        /// <summary>
//        /// 
//        /// </summary>
//        protected int[,] m_aLabel = null; //This is for assigning labels in the first step
        
//        /// <summary>
//        /// since sometimes in aLabel we get different ids for same objects,
//        /// this table is used.
//        /// </summary>
//        protected int[] m_aLabelTable = null;
        
//        /// <summary>
//        /// 
//        /// </summary>
//        protected UnsafeBitmap m_Bitmap = null;
//        protected int m_nWidth = -1;
//        protected int m_nHeight = -1;

//        /// <summary>
//        /// 
//        /// </summary>
//        protected Blob[] m_aDetectedBlob = new Blob[]{};

//        /// <summary>
//        /// 
//        /// </summary>
//        protected Color m_ForegroundColor = Definitions.White;

//        /// <summary>
//        /// Initializes a new instance of the <see cref="BlobExtractor"/> class.
//        /// </summary>
//        /// <param name="_bitmap">The _bitmap.</param>
//        public BlobExtractor(UnsafeBitmap _bitmap)
//        {
//            m_Bitmap = _bitmap;

//            m_nWidth = m_Bitmap.Width;
//            m_nHeight = m_Bitmap.Height;
//            m_Bitmap = _bitmap;
//            m_aLabel = new int[m_nWidth, m_nHeight];

//            // The maximum number of blobs is given by an image filled with equally spaced single pixel
//            // blobs. For images with less blobs, memory will be wasted, but this approach is simpler and
//            // probably quicker than dynamically resizing arrays
//            int tableSize = m_nWidth * m_nHeight / 4;
//            m_aLabelTable = new int[tableSize];
//        }

//        /// <summary>
//        /// Detects this instance.
//        /// </summary>
//        /// <returns></returns>
//        public Blob[] Detect()
//        {
//            int nNum = 0;
//            int nLabelCount = 1; // Initial Label number

//            int x = 0;
//            int y = 0;

//            int nLabelTableIndex = 0;

//            Color color = Color.Transparent;
//            for (y = 1; y < m_nHeight - 1; y++)
//            {
//                for (x = 1; x < m_nWidth - 1; x++)
//                {
//                    color = m_Bitmap.GetPixel(x, y);

//                    //if (x - 1 < 0 || y - 1 < 0 || x + 1 > nWidth - 1) continue;
//                    if (color != m_ForegroundColor)
//                    {
//                        continue;
//                    }

//                    int a = m_aLabel[x - 1, y - 1];
//                    int b = m_aLabel[x, y - 1];
//                    int c = m_aLabel[x + 1, y - 1];
//                    int d = m_aLabel[x - 1, y];

//                    if (a == 0       //a
//                        && b == 0        //b
//                        && c == 0    //c
//                        && d == 0)       //d
//                    {
//                        m_aLabel[x, y] = nLabelCount;
//                        m_aLabelTable[nLabelTableIndex] = nLabelCount;
//                        nLabelCount++;
//                        nLabelTableIndex++;
//                    }
//                    else
//                    {
//                        nNum = GetMinimum(new int[] { a, b, c, d });

//                        if (a != 0) m_aLabelTable[a] = nNum;
//                        if (b != 0) m_aLabelTable[b] = nNum;
//                        if (c != 0) m_aLabelTable[c] = nNum;
//                        if (d != 0) m_aLabelTable[d] = nNum;
                        
//                        if (nNum > 0)
//                        {
//                            m_aLabel[x - 1, y - 1] = m_aLabel[x, y - 1] = m_aLabel[x + 1, y - 1] = m_aLabel[x - 1, y] = nNum;
//                        }
//                    }
//                }
//            }

//            CreateBlobsFromLabelTable(nLabelCount);
//            return m_aDetectedBlob;
//        }

//        /// <summary>
//        /// Creates the blobs from label table.
//        /// </summary>
//        /// <param name="nLabelCount">The n label count.</param>
//        /// <returns></returns>
//        private Blob[] CreateBlobsFromLabelTable(int nLabelCount)
//        {
//            m_aDetectedBlob = new Blob[nLabelCount];

//            Dictionary<int, Blob> aBlobPoint = new Dictionary<int, Blob>();

//            int x = 0;
//            int y = 0;

//            for (y = 0; y < m_nHeight; y++)
//            {
//                for (x = 0; x < m_nWidth; x++)
//                {
//                    int nLabelKey = m_aLabel[x, y];
//                    if (!aBlobPoint.ContainsKey(nLabelKey))
//                    {
//                        Blob newBlob = new Blob(nLabelKey);
//                        aBlobPoint.Add(nLabelKey, newBlob);
//                    }
//                    else
//                    {
//                        aBlobPoint[nLabelKey].m_aPoint.Add(new Point(x, y));
//                    }
//                }
//            }

//            //merge blobs
//            for (int i = 0; i < m_aLabelTable.Length; i++)
//            {
//                //aBlobPoint[i].m_aPoint.AddRange(aBlobPoint[m_aLabelTable[i]].m_aPoint);
//                //aBlobPoint.Remove(m_aLabelTable[i]);
//            }

//            int nIndex = 0;
//            foreach (KeyValuePair<int, Blob> keyValuePair in aBlobPoint)
//            {
//                m_aDetectedBlob[nIndex++] = keyValuePair.Value;
//            }
//            return m_aDetectedBlob;
//        }

//        /// <summary>
//        /// Gets the minimum.
//        /// </summary>
//        /// <param name="aLabel">A label.</param>
//        /// <returns></returns>
//        public int GetMinimum(int[] aLabel)
//        {
//            Array.Sort(aLabel);
//            foreach (int i in aLabel)
//            {
//                if (i > 0) return i;
//            }
//            return -1;
//        }

//        #region IEnumerable Members

//        /// <summary>
//        /// Returns an enumerator that iterates through a collection.
//        /// </summary>
//        /// <returns>
//        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
//        /// </returns>
//        public IEnumerator GetEnumerator()
//        {
//            return m_aDetectedBlob.GetEnumerator();
//        }

//        #endregion
//    }
//}
